package com.linwen.excise.leetcode;

/**
 * @author : linwen
 * @date : 下午12:34 2021/4/5
 */
public class _66加一 {
    public static void main(String[] args) {
        int[] arr = new int[]{9,9,9};
        int[] ints = plusOne(arr);
        for(int num:ints){
            System.out.print(num+"  ");
        }
        int[] ints2 = plusOne(arr);
        for(int num:ints2){
            System.out.print(num+"  ");
        }
    }

    public int[] plusOne2(int[] digits) {
        // +1
        for (int i = digits.length - 1; i >= 0; i--) {
            digits[i]++;
            digits[i] = digits[i] % 10;
            if (digits[i] != 0) {
                return digits;
            }
        }
        // 99  999 等特殊才会产生进位，且结果只能为100 1000 10000
        digits = new int[digits.length + 1];
        digits[0] = 1;
        return digits;
    }
    public static int[] plusOne(int[] digits) {
        int count = 0;
        for (int num:digits){
            if(num==9){
                count++;
            }
        }
        if(count == digits.length){
            int[] ints = new int[count+1];
            ints[0]=1;
            return ints;
        }
        digits[digits.length-1] = digits[digits.length - 1] + 1;
        for (int i = digits.length-1; i >=0; i--) {
            if(digits[i]==10){
                digits[i] = digits[i]%10;
                digits[i-1]++;
            }
        }
        return digits;
    }
}
